package com.baidu.controller;

import com.baidu.constant.MessageConstant;
import com.baidu.pojo.CheckItem;
import com.baidu.pojo.PageResult;
import com.baidu.pojo.QueryPageBean;
import com.baidu.pojo.Result;
import com.baidu.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * controller层代码
 */
@Controller
@ResponseBody
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckItem> all = checkItemService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, all);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
//        int ii = 1/0;
        int i = checkItemService.add(checkItem);
        if (i>0){
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }else {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    // 分页查询, 条件查询
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<CheckItem> pageResult = checkItemService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    // 删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        int result = checkItemService.delete(id);
        if (result > 0){
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
    }

    // 回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    // 更新
    @RequestMapping("/update")
    public Result update(@RequestBody CheckItem checkItem){
        int id = checkItemService.update(checkItem);
        if (id > 0){
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }else {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

}
